package com.pan.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MultiThreadSafe {

    /**
     * 创建固定数量线程池
     */
    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);

    Object lock = new Object();

    public void sout(String id){
        fixedThreadPool.execute(new Runnable() {
            public void run() {
                synchronized(lock) {
                    try {
                        System.out.println("id是："+id+"当前线程是："+Thread.currentThread().getName());
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                }
            }
        });
    }

    public void soutFor(){
        for(int i=0;i<100;i++){
            String id = "id-id-id"+i%2;
            //if(i%2==0){
                sout(id);
            //}
        }
    }

    public static void main(String[] args) {
        MultiThreadSafe multiThreadSafe = new MultiThreadSafe();
        multiThreadSafe.soutFor();
    }


    public String sendApproveCallBack( String id) {

        try {
            fixedThreadPool.execute(new Thread() {
                public void run() {
                    synchronized(id) {
                        System.out.println("id是："+id+"当前线程是："+Thread.currentThread().getName());
                    }
                }
            });
            return "ResultData";
        } catch (Exception e) {
            e.printStackTrace();
            return "ResultData";
        }
    }

}
